package com.ctsi.ssdc.web;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.lang.Integer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctsi.ssdc.service.LitemallAdminService;
import com.ctsi.ssdc.domain.LitemallAdmin;

import com.ctsi.ssdc.exception.BadRequestAlertException;
import com.ctsi.ssdc.model.PageResult;
import com.ctsi.ssdc.util.HeaderUtil;
import com.ctsi.ssdc.util.ResponseUtil;


/**
 * REST controller for managing LitemallAdmin.
 *
 * @author ctsi-biyi-generator
 *
 */
@Api(value = "/api",tags = {"litemall-admin-controller"})
@RestController
@RequestMapping("/api")
public class LitemallAdminController {

    private final Logger log = LoggerFactory.getLogger(LitemallAdminController.class);

    private static final String ENTITY_NAME = "litemallAdmin";

    private final LitemallAdminService litemallAdminService;

    public LitemallAdminController(LitemallAdminService litemallAdminService) {
        this.litemallAdminService = litemallAdminService;
    }

    @InitBinder   
    public void initBinder(WebDataBinder binder) {   
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
        dateFormat.setLenient(true);   
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   
    } 
   
    /**
     * POST  /litemallAdmins : Create a new litemallAdmin.
     *
     * @param litemallAdmin the litemallAdmin to create
     * @return the ResponseEntity with status 201 (Created) and with body the new litemallAdmin,
     * or with status 400 (Bad Request) if the litemallAdmin has already an id
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallAdmin",name = "litemallAdmin",value = "the litemallAdmin to create")
    })
    @ApiOperation(value = "POST  /litemallAdmins : create a new litemallAdmin.",
        notes = "POST  /litemallAdmins : create a new litemallAdmin.",httpMethod = "POST")
    @PostMapping("/litemallAdmins")
    public ResponseEntity<LitemallAdmin> createLitemallAdmin(@RequestBody LitemallAdmin litemallAdmin) throws URISyntaxException {
        log.debug("REST request to save LitemallAdmin : {}", litemallAdmin);
        if (litemallAdmin.getId() != null) {
            throw new BadRequestAlertException("A new litemallAdmin cannot already have an id", ENTITY_NAME, "idexists");
        }
        LitemallAdmin result = litemallAdminService.insert(litemallAdmin);
        return ResponseEntity.created(new URI("/api/litemallAdmins" + "/" +result.getId() ))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }
	
    /**
     * PUT  /litemallAdmins : Updates an existing litemallAdmin.
     *
     * @param litemallAdmin the litemallAdmin to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated litemallAdmin,
     * or with status 400 (Bad Request) if the litemallAdmin is not valid,
     * or with status 500 (Internal Server Error) if the litemallAdmin couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "body",dataType = "LitemallAdmin",
        name = "litemallAdmin",value = "the litemallAdmin to update")
    })
    @ApiOperation(value = "PUT  /litemallAdmins : updates an existing litemallAdmin.",
        notes = "PUT  /litemallAdmins : updates an existing litemallAdmin.",httpMethod = "PUT")
    @PutMapping("/litemallAdmins")
    public ResponseEntity<LitemallAdmin> updateLitemallAdmin(@RequestBody LitemallAdmin litemallAdmin) throws URISyntaxException {
        log.debug("REST request to update LitemallAdmin : {}", litemallAdmin);
	    if (litemallAdmin.getId() == null) {
	    	return createLitemallAdmin(litemallAdmin);
	    }
        LitemallAdmin result = litemallAdminService.update(litemallAdmin);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * GET  /litemallAdmins/:id : get the "id" litemallAdmin.
     *
     * @param id the id of the litemallAdmin to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the litemallAdmin, or with status 404 (Not Found)
     */
//    @ApiImplicitParams({
//        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",name = "id",
//        value = "the id of the litemallAdmin to retrieve")
//    })
//    @ApiOperation(value = "GET  /litemallAdmins/id : get the id litemallAdmin.",
//        notes = "GET  /litemallAdmins/id : get the id litemallAdmin.",httpMethod = "GET")
//    @GetMapping("/litemallAdmins/{id}")
//    public ResponseEntity<LitemallAdmin> getLitemallAdmin(@PathVariable Integer id) {
//        log.debug("REST request to get LitemallAdmin : {}", id);
//        LitemallAdmin litemallAdmin = litemallAdminService.findOne(id);
//        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(litemallAdmin));
//    }

    /**
    * GET  /litemallAdmins : get the litemallAdmins firstStringBaseColumn.
    *
    * @return the ResponseEntity with status 200 (OK) and the list of litemallAdmins in body
    */
    @ApiOperation(value = "GET  /litemallAdmins : get the litemallAdmins firstStringBaseColumn.",
                notes = "GET  /litemallAdmins : get the litemallAdmins firstStringBaseColumn.",httpMethod = "GET")
    @GetMapping("/litemallAdmins")
    public PageResult<LitemallAdmin> getLitemallAdminsList(String username ,Pageable pageable) {
        log.debug("REST request to get LitemallAdminsList");
        return litemallAdminService.findFirstStringColumn(username ,pageable);
    }
	
    /**
     * DELETE  /litemallAdmins/:id : delete the "id" litemallAdmin.
     *
     * @param id the id of the litemallAdmin to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",dataType = "INTEGER",
            name = "id",value = "the id of the litemallAdmin to delete")
    })
    @ApiOperation(value = "DELETE  /litemallAdmins/id : delete the id litemallAdmin.",
        notes = "DELETE  /litemallAdmins/id : delete the id litemallAdmin.",httpMethod = "DELETE")
    @DeleteMapping("/litemallAdmins/{id}")
    public ResponseEntity<Void> deleteLitemallAdmin(@PathVariable Integer id) {
        log.debug("REST request to delete LitemallAdmin : {}", id);
        litemallAdminService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    
}
